
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

#include "msgpp.hpp"

#define MSG_ERROR_MESSAGE	"[ error ]"
#define MSG_WARNING_MESSAGE	"[ warning ]"
#define MSG_NOTICE_MESSAGE	"[ notice ]"

#define MSG_SEP " >> "

static void __notice_prefix(void) {
	imp::Msg::reset();
	imp::Msg::setAttributes(imp::Msg::ATTR_BRIGHT, imp::Msg::NOTICE_COLOR, imp::Msg::COLOR_BLACK);
	fprintf(stderr, MSG_NOTICE_MESSAGE);
	imp::Msg::reset();
	fprintf(stderr, MSG_SEP);
}

static void __warning_prefix(void) {
	imp::Msg::reset();
	imp::Msg::setAttributes(imp::Msg::ATTR_BRIGHT, imp::Msg::WARNING_COLOR, imp::Msg::COLOR_BLACK);
	fprintf(stderr, MSG_WARNING_MESSAGE);
	imp::Msg::reset();
	fprintf(stderr, MSG_SEP);
}

static void __error_prefix(void) {
	imp::Msg::reset();
	imp::Msg::setAttributes(imp::Msg::ATTR_BRIGHT, imp::Msg::ERROR_COLOR, imp::Msg::COLOR_BLACK);
	fprintf(stderr, MSG_ERROR_MESSAGE);
	imp::Msg::reset();
	fprintf(stderr, MSG_SEP);
}


// = Class Implementation ======================================================

namespace imp {

	void Msg::setAttributes(Msg::msgAttrib attrib, Msg::msgColor fg, Msg::msgColor bg) {
		char command[13];
		sprintf(command, "%c[%d;%d;%dm", 0x1B, attrib, fg + 30, bg + 40);
		fprintf(stderr, "%s", command);
	}

	void Msg::reset(void) {
		Msg::setAttributes(Msg::ATTR_RESET, Msg::DEFAULT_FOREGROUND, Msg::DEFAULT_BACKGROUND);
	}

	void Msg::endl(void) {
		fprintf(stderr, "\n");
	}

	void Msg::endl(int n) {
		for (; n > 0; --n) fprintf(stderr, "\n");
	}

	void Msg::notice(const char* msg, ...) {

		__notice_prefix();

		va_list ap;
		va_start(ap, msg);
		vfprintf(stderr, msg, ap);
		va_end(ap);

	}

	void Msg::warning(const char* msg, ...) {

		__warning_prefix();

		va_list ap;
		va_start(ap, msg);
		vfprintf(stderr, msg, ap);
		va_end(ap);

	}

	void Msg::error(const char* msg, ...) {

		__error_prefix();

		va_list ap;
		va_start(ap, msg);
		vfprintf(stderr, msg, ap);
		va_end(ap);

		exit(EXIT_FAILURE);

	}

}